﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Sonic.Framework.Interfaces;
using Sonic.Framework.Enums;
using System.Xml;

namespace Sonic.Framework.ProcessorToolDirectives.Default
{
    /// <summary>
    /// Date stamp files
    /// </summary>
    public class DateStamper : IPluginBase, IProcessorToolDirective
    {
        private const string _name = "DateStamper";
        public string Name
        {
            get { return _name; }
        }

        private Dictionary<string, string> _attributes;
        public Dictionary<string, string> Attributes
        {
            get
            {
                return _attributes;
            }
        }

        /// <summary>
        /// The DateTime format string that will be used to date stamp file names
        /// </summary>
        private string _dateStampFormat = "yyMMdd";
        public string DateStampFormat
        {
            get
            {
                return _dateStampFormat;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public DateStamper()
        {
            this._attributes = new Dictionary<string, string>();
        }

        public bool Execute(ISonicProcessor processor, ISiteDirectory siteDir, ref List<FileInfo> directoryFileInfos, XmlDocument argumentsXml)
        {
            _dateStampFormat = GetDirectiveCallAttributeFromArgXml(argumentsXml, "dateFormat");

            bool deleteSourceFiles = false;
            try
            {
                deleteSourceFiles = bool.Parse(GetDirectiveCallAttributeFromArgXml(argumentsXml, "deleteSourceFiles"));
            }
            catch { }

            foreach (FileInfo fi in directoryFileInfos)
            {
                if (fi.Exists)
                {
                    string dest = DateStampFileName(fi, DateStampFormat);

                    fi.CopyTo(dest, true);

                    // Track updates to file names that will need to be changed in content files
                    FileReferenceUpdate renamedFileRefUpdate = new FileReferenceUpdate();
                    renamedFileRefUpdate.FileReferenceUpdateType = FileReferenceUpdateType.Moved;
                    renamedFileRefUpdate.From.Add(fi);
                    renamedFileRefUpdate.To = new FileInfo(dest);
                    processor.AddFileReferenceUpdate(renamedFileRefUpdate);

                    if (deleteSourceFiles)
                    {
                        fi.Delete();
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// Returns the date stamped version of the file path
        /// </summary>
        /// <param name="file"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        private string DateStampFileName(FileInfo file, string format)
        {
            string dateStamp = "-" + DateTime.Now.ToString(format);
            string path = file.FullName;

            path = path.Substring(0, path.Length - file.Extension.Length) + dateStamp + file.Extension;

            return path;
        }

    }
}
